package com.mouse.dnf;

import com.mouse.damo.micro.Com;
import com.mouse.damo.micro.Sleep;
import com.mouse.dnf.service.ChanDouService;
import lombok.Getter;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class DnfApplication extends JDialog {

    private final JPanel contentPanel = new JPanel();
    @Getter
    static JTextArea textArea;
    JLabel label_1;
    JButton button_1;

    @Getter
    Com com;
    static ChanDouService chanDouService = null;
    private static boolean open = false;
    private static boolean firstStart = true;

    private ChanDouService getCd() {
        if (chanDouService == null) chanDouService = new ChanDouService();
        return chanDouService;
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            DnfApplication dialog = new DnfApplication();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public DnfApplication() {
        setTitle("颤抖的大地");
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);

        JLabel label = new JLabel("检查客户端:");

        label_1 = new JLabel("不存在");

        JButton button = new JButton("检查");
        button.addActionListener(e -> check());
        button_1 = new JButton("开始");
        button_1.addActionListener(e -> start());
        JScrollPane scrollPane = new JScrollPane();
        GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
        gl_contentPanel.setHorizontalGroup(
                gl_contentPanel.createParallelGroup(Alignment.LEADING)
                        .addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
                                .addContainerGap(68, Short.MAX_VALUE)
                                .addComponent(label)
                                .addGap(18)
                                .addComponent(label_1)
                                .addGap(69)
                                .addComponent(button)
                                .addGap(110))
                        .addGroup(gl_contentPanel.createSequentialGroup()
                                .addGap(174)
                                .addComponent(button_1)
                                .addContainerGap(193, Short.MAX_VALUE))
                        .addGroup(gl_contentPanel.createSequentialGroup()
                                .addGap(12)
                                .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                                .addContainerGap())
        );
        gl_contentPanel.setVerticalGroup(
                gl_contentPanel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPanel.createSequentialGroup()
                                .addGap(32)
                                .addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label)
                                        .addComponent(label_1)
                                        .addComponent(button))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(button_1)
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                                .addContainerGap())
        );

        textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        scrollPane.setViewportView(textArea);
        contentPanel.setLayout(gl_contentPanel);
    }

    private void start() {
        if (com == null) {
            JOptionPane.showMessageDialog(this, "请检查客户端");
            return;
        }
        ChanDouService service = getCd();
        if (!service.checkHwnd(com.getHwnd())) {
            label_1 = new JLabel("不存在");
            JOptionPane.showMessageDialog(this, "请重新检查客户端");
            return;
        }
        if (firstStart) {
            new Thread(() -> service.auto(com)).start();
            firstStart = false;
        }
        if (!open) {
            button_1.setText("关闭");
            DnfApplication.setLog("开始");
            new Thread(() -> {
                for (int i = 3; i >= 0; i--) {
                    setLog("倒计时(s):" + i);
                    Sleep.sleep(1000);
                }
            }).start();
            service.setStart(true);
            open = true;
        } else {
            button_1.setText("开始");
            DnfApplication.setLog("关闭");
            service.setStart(false);
            open = false;
        }

    }

    private void check() {
        ChanDouService cd = getCd();
        Com bindCom = cd.findHwnd();
        if (bindCom == null) {
            label_1.setText("失败");
            setLog("失败");
            return;
        }
        label_1.setText("成功");
        setLog("成功");
        com = bindCom;
    }

    public static void setLog(String str) {
        int lineCount = textArea.getLineCount();
        if (lineCount >= 1000) textArea.setText("");
        lineCount = textArea.getLineCount();
        textArea.append(lineCount + "--" + str + "\n");
    }
}
